<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="项目ID/项目标题/项目内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="项目时间" prop="publishTime">
          <el-date-picker 
          clearable
            v-model="filterForm.publishTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户" prop="clieneteteID">
          <el-input v-model="filterForm.clieneteteID" clearable placeholder="客户ID/客户电话/客户姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">已处理</el-option>
              <el-option label="6" value="5">待处理</el-option>
              <el-option label="1" value="1">处理中</el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      height="620"
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="140">
        <template slot-scope="{row}">
          <span>{{ row.symbol }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版本" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发行日志" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status.value == 6" size="mini" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value == 5 " size="mini" @click="handleModifyStatus(row,6)">
            启用2432432
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">
              新建
            </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />

  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      connected:"loading",
      filterForm: {
        start: 1,
        perpage: 10,
        title: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.init()
  },
  destroyed(){
    this.close()
  },
  methods: {
    init: function () {
      if(typeof(WebSocket) === "undefined"){
          alert("您的浏览器不支持socket")
      }else{
          // 实例化socket
          this.socket = new WebSocket(getHost())
          // 监听socket连接
          this.socket.onopen = this.open
          // 监听socket错误信息
          this.socket.onerror = this.error
          // 监听socket消息
          this.socket.onmessage = this.getMessage
      }
    },
    open: function () {

        this.connected = 'loaded'

        this.getList()
    },
    error: function (e) {
        console.log("连接错误",JSON.stringify(e))
    },
    getMessage: function (receiveData) {
      receiveData = JSON.parse(receiveData.data)
      if(receiveData.status != 200){
          console.log(receiveData.msg)
      }else{
        this.list = receiveData.data.list
        this.total = receiveData.data.total
      }
    },
    send: function (params) {
      params = JSON.stringify(params)
      this.socket.send(params)
    },
    close: function () {
        console.log("socket已经关闭")
    },
    destroyed () {
        // 销毁监听
        this.socket.onclose = this.close
    },
    getList:function(){
      this.send({
        "conversationType":"finance.bankcard.list",
        "target":{"id":"3"}
      })
    },
    handleShow:function(item){
      this.$router.push({ path: 'faultiness-detail', query: { 'faultinessID': item.ID }})
    },
    handleCreate:function(){
      this.$router.push({ path: 'faultiness-add'})
    },
    handleUpdate:function(item){
      this.$router.push({ path: 'faultiness-add', query: { 'faultinessID': item.ID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    handleDelete(row, index) {
      removeFaultinessByID(row.ID).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
<style>

</style>